<%@ page
	import="merlin.beans.*,merlin.dao.*,merlin.managers.*,merlin.servlets.*,merlin.utils.*"
	contentType="text/html;charset=windows-1252"
%>

<%
	// let the user access the system only if he is logged in.
	if(session.getAttribute("IWusername") == null){
		response.sendRedirect("IndustryWatchLogin.jsp");
		return;
	}
%>

<html>
  <head>
	<meta HTTP-EQUIV="Cache-Control"  CONTENT="no-cache" />
	<meta HTTP-EQUIV="Expires"        CONTENT="1 Jan 1980" />
	<meta HTTP-EQUIV="MaxAge"         CONTENT="0" />
	<meta HTTP-EQUIV="Pragma-Control" CONTENT="no-cache" />

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Industry Watch - Change Password</title>

	<!-- begin DWR imports -->
	<script type='text/javascript' src='dwr/interface/IndustryWatchManager.js'></script>
	<script type='text/javascript' src='dwr/engine.js'></script>
	<script type='text/javascript' src='dwr/util.js'></script>
	<!-- ends DWR imports -->

	<!-- begin Prototype import -->
	<script type='text/javascript' src='mr_scripts/prototype.js'></script>
	<!-- ends Prototype import -->

    <script>

		function isBlank(str){
			var len = str.length;
			if(len==0)return true;	// zero-length string
			for(i=0;i<len;i++){
				if(str.charAt(i) !=' ' && str.charAt(i) !='\n')return false; //some character is there
			}
			return true;
		}

    	function changePassword(txtOldPassword,txtPassword1,txtPassword2){
    		var oldPwd = txtOldPassword.value.toLowerCase();
    		var newPwd = txtPassword1.value.toLowerCase();
    		var newPwd2 = txtPassword2.value.toLowerCase();

    		if(isBlank(newPwd)){
    			alert("Password cannot be blank");
    			txtPassword1.focus();
    			return false;
    		}
    		if(newPwd != newPwd2){
    			alert("The new passwords donot match");
    			txtPassword1.focus();
    			return false;
    		}

			if(newPwd.length < 8 || newPwd.length > 12){
				alert("The password should be between 8 & 12 characters long and contain a combination of letters and numbers");
				return false;
			}

			// letters & digits
			if (! newPwd.match(/[a-z]/) || ! newPwd.match(/[0-9]/)){
				alert("The password should be between 8 & 12 characters long and contain a combination of letters and numbers");
				return false;
			}

			<%
				IWCustomerDetailsBean userBean = (IWCustomerDetailsBean)session.getAttribute("IWUserDetails");
				if(! userBean.isTrialIndicator()){
			%>
				//Make DWR remote call.
    	        IndustryWatchManager.changePassword(<%=userBean.getCustomerDetailsId() %>,oldPwd,newPwd,handleChangePassword);
			<%
				}else{
			%>
				handleChangePassword(true);
			<%
				}
			%>
    	}

		function handleChangePassword(data){
			if(data == true){
				alert("Password changed !");
				document.location.href='iwMainPage.jsp';
			}else{
				$("spnMessage").innerHTML = "Old password is incorrect";
			}
		}

		function logoutIWUser(frm){
			document.location.href="industryWatch?mode=logoutFromIndustryWatch";
		}

    </script>

	<link href="css/master.css" rel="stylesheet" type="text/css">

  </head>

<body onload="document.loginForm.txtOldPassword.focus()">

	<jsp:include page = "iwHeaderSmall.jsp"/>

	<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
		<tr>
			<td>
				<div style="height:.5cm;">
				</div>
				<div align='center' style="height:15px;">
					<span id="spnMessage" align='center' STYLE='FONT: 11px verdana,arial,sans-serif;color:red;font-weight:bold'></span>
				</div>
				<p>
				</p>
				<div align='center' style="height:300">
				<form name="loginForm" onsubmit="changePassword(txtOldPassword,txtNewPassword,txtNewPassword2);return false;">

					<table width="60%" border="0"> <!-- style="background-color:rgb(146,207,100);font-family:tahoma;" -->
						<tr>
							<td width="40%" style='font:11px verdana,arial,sans-serif;'>
							 &nbsp;Old Password
							</td>
							<td width="70%">
								<input type="password" name="txtOldPassword" style="text-transform:lowercase;font:11px verdana,arial,sans-serif;width:200" value= '' >
							</td>
						</tr>
						<tr>
							<td width="40%" style='font:11px verdana,arial,sans-serif;'>
							 &nbsp;New password
							</td>
							<td width="70%">
								<input type="password" name="txtNewPassword" style="text-transform:lowercase;font:11px verdana,arial,sans-serif;width:200" value= '' >
							</td>
						</tr>
						<tr>
							<td width="40%" style='font:11px verdana,arial,sans-serif;'>
							 &nbsp;Re-enter New password
							</td>
							<td width="70%">
								<input type="password" name="txtNewPassword2" style="text-transform:lowercase;font:11px verdana,arial,sans-serif;width:200" value= '' >
							</td>
						</tr>
						<tr>
							<td>
							</td>
							<td>
								<input type="button" name="btnCancel" value="Cancel" onclick ="history.back()" style="margin-right:20px;font: 11px verdana,arial, sans-serif ;width:90">
								<input type="SUBMIT" name="btnChange" value="Change Password" style="margin-right:20px;font: 11px verdana,arial, sans-serif ;width:110">
							</td>
						</tr>
					</table>
				</form>
				</div>
			</td>
		</tr>
	</table>

	<div style="height:1.0cm;">
	</div>
	<jsp:include page="mr_footer.jsp"/>
  </body>
</html>